<template>
  <div class="app-container ">
    <el-form ref="form" size="mini" :model="form" :rules="rules" label-width="110px">
      <el-tabs>
        <el-row>
          <el-col :span="24">
            <el-form-item label="版本号:" prop="region">
              <el-input v-model="form.version" auto-complete="off" clearable placeholder="请输入版本号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="描述:">
              <el-input
                v-model="form.desctext"
                type="textarea"
                auto-complete="off"
                :autosize="{ minRows: 6, maxRows: 10}"
                clearable
                placeholder="请输入描述"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <el-button size="mini" type="primary" :loading="loading" @click="submit">上传小程序</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-tabs>
    </el-form>
    <div>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :row-class-name="rowClass"
        row-key="id"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        :border="false"
        :stripe="true"
        class="eltable"
        :data="list"
        style="width: 100%"
        @selection-change="selection"
      >
        <el-table-column align="center" type="selection" width="42" />
        <el-table-column
          align="left"
          property="version"
          label="版本"
          show-overflow-tooltip
          width=""
        />
        <el-table-column
          align="left"
          property="desctext"
          label="描述"
          show-overflow-tooltip
          width=""
        />
        <el-table-column
          align="left"
          property="update_time"
          label="上传时间"
          show-overflow-tooltip
          width=""
        />
        <el-table-column
          align="center"
          property="status"
          label="开启审核"
          show-overflow-tooltip
          width=""
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="0"
              :inactive-value="1"
              @change="listUpdate(scope.row,'status')"
            />
          </template>

        </el-table-column>
      </el-table>
      <Pagination
        :total="page_data.total"
        :page.sync="page_data.page"
        :limit.sync="page_data.limit"
        @pagination="index"
      />
    </div>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
export default {
  name: 'Configuploadminiprogram',
  components: { Pagination },
  data() {
    return {
      form: {},
      ids: [],
      list: [],
      page_data: {
        limit: 20,
        page: 1,
        total: 20
      },
      sysversion: '',
      loading: false,
      rules: {
        version: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.$api.get('/Sysbase/sysversion', this.form).then(res => {
      this.sysversion = res.data
    })
    this.getInfo()
    this.index()
  },
  methods: {
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.form.path = this.$route.path
          this.$api.post('/uploadminiprogram/update', this.form).then(res => {
            this.form = JSON.stringify(res.data) == '[]' ? {} : res.data
            this.$message({
              message: '上传成功',
              type: 'success'
            })
            this.getInfo()
            this.index()
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    getInfo() {
      this.$api.post('/uploadminiprogram/getInfo', {
        path: this.$route.path
      }).then(res => {
        this.form = JSON.stringify(res.data) == '[]' ? {} : res.data
        this.setDefaultVal('keyword')
      })
    },
    setDefaultVal(key) {
      if (this.form[key] == null || this.form[key] == '') {
        this.form[key] = []
      }
    },
    index() {
      const param = {
        path: this.$route.path,
        limit: this.page_data.limit,
        page: this.page_data.page
      }
      Object.assign(param, this.searchData)
      this.loading = true

      this.$api.post('/uploadminiprogram/index', param).then(res => {
        this.list = res.data.data
        this.page_data.total = res.data.total
        this.loading = false
        // console.log(this.page_data);
        if (this.page_data.page == 1) {
          this.searchForm = [
            {
              type: 'Input',
              label: '关键词',
              prop: 'keyword',
              width: '230px'
            }
          ]
        }
      })
    },
    listUpdate(row, field) {
      if (row.id) {
        this.$api.post('/uploadminiprogram/listUpdate', {
          id: row.id,
          [field]: row[field]
        }).then(res => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        })
      }
    },
    selection(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    rowClass({
      row,
      rowIndex
    }) {
      for (let i = 0; i < this.ids.length; i++) {
        if (row.id === this.ids[i]) {
          return 'rowLight'
        }
      }
    }
  }
}
</script>
